package com.paas.form.service.impl;

import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.common.web.service.BaseServiceImpl;
import com.paas.form.dao.FormGroupFinalDao;
import com.paas.form.entity.FormGroupFinalEntity;
import com.paas.form.entity.FormViewPubEntity;
import com.paas.form.enums.GroupTypeEnums;
import com.paas.form.service.IFormGroupFinalService;
import com.paas.form.service.IFormViewPubService;
import com.paas.form.vo.FormGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("formGroupFinalService")
public class FormGroupFinalServiceImpl extends BaseServiceImpl<FormGroupFinalDao, FormGroupFinalEntity> implements IFormGroupFinalService {
    @Autowired
    private IFormViewPubService formViewPubService;

    /**
     * 查询定版分组树
     * @return {@link List}<{@link FormGroupVo}>
     */
    @Override
    public List<FormGroupVo> queryFinalGroupAndViewTree() {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormGroupFinalEntity> finalEntities = this.list();
        if (BeanUtil.isEmpty(finalEntities)) {
            return groupVos;
        }
        //groupEntities转map
        Map<String, FormGroupFinalEntity> finalEntityMap = finalEntities.stream().collect(Collectors.toMap(FormGroupFinalEntity::getId, finalEntity -> finalEntity));
        List<FormViewPubEntity> viewPubEntities = formViewPubService.list();
        //finalEntities 过滤出根节点
        List<FormGroupFinalEntity> rootFinalGroups = finalEntities.stream().filter(finalEntity -> StringUtil.isEmpty(finalEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(rootFinalGroups)) {
            //递归查询子节点
            for (FormGroupFinalEntity rootFinalGroup : rootFinalGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(rootFinalGroup.getId());
                groupVo.setCode(rootFinalGroup.getCode());
                groupVo.setName(rootFinalGroup.getName());
                groupVo.setPath(rootFinalGroup.getPath());
                groupVo.setRemark(rootFinalGroup.getRemark());
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setParentId(rootFinalGroup.getParentId());
                String parentName = StringUtil.isNullOrEmpty(rootFinalGroup.getParentId()) ? "" : finalEntityMap.get(rootFinalGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setChildren(getChildrenGroupAndView(rootFinalGroup, finalEntities, viewPubEntities, finalEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    @Override
    public List<FormGroupVo> queryFinalGroupTree() {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormGroupFinalEntity> finalEntities = this.list();
        if (BeanUtil.isEmpty(finalEntities)) {
            return groupVos;
        }
        //groupEntities转map
        Map<String, FormGroupFinalEntity> finalEntityMap = finalEntities.stream().collect(Collectors.toMap(FormGroupFinalEntity::getId, finalEntity -> finalEntity));
        //finalEntities 过滤出根节点
        List<FormGroupFinalEntity> rootFinalGroups = finalEntities.stream().filter(finalEntity -> StringUtil.isEmpty(finalEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(rootFinalGroups)) {
            //递归查询子节点
            for (FormGroupFinalEntity rootFinalGroup : rootFinalGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(rootFinalGroup.getId());
                groupVo.setCode(rootFinalGroup.getCode());
                groupVo.setName(rootFinalGroup.getName());
                groupVo.setPath(rootFinalGroup.getPath());
                groupVo.setRemark(rootFinalGroup.getRemark());
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                String parentName = StringUtil.isNullOrEmpty(rootFinalGroup.getParentId()) ? "" : finalEntityMap.get(rootFinalGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setParentId(rootFinalGroup.getParentId());
                groupVo.setChildren(getChildrenGroup(rootFinalGroup, finalEntities, finalEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询子节点
     * @param rootFinalGroup 根节点
     * @param finalEntities  定版分组实体
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenGroup(FormGroupFinalEntity rootFinalGroup,
                                               List<FormGroupFinalEntity> finalEntities,
                                               Map<String, FormGroupFinalEntity> finalEntityMap) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        //finalEntities 过滤出根节点
        List<FormGroupFinalEntity> childrenFinalGroups =
                finalEntities.stream().filter(finalEntity -> rootFinalGroup.getId().equals(finalEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(childrenFinalGroups)) {
            //递归查询子节点
            for (FormGroupFinalEntity childrenFinalGroup : childrenFinalGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(childrenFinalGroup.getId());
                groupVo.setCode(childrenFinalGroup.getCode());
                groupVo.setName(childrenFinalGroup.getName());
                groupVo.setPath(childrenFinalGroup.getPath());
                groupVo.setRemark(childrenFinalGroup.getRemark());
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setParentId(childrenFinalGroup.getParentId());
                String parentName = StringUtil.isNullOrEmpty(childrenFinalGroup.getParentId()) ? "" : finalEntityMap.get(childrenFinalGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setChildren(getChildrenGroup(childrenFinalGroup, finalEntities, finalEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询子节点
     * @param rootFinalGroup  根节点
     * @param finalEntities   定版分组实体
     * @param viewPubEntities 定版视图实体
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenGroupAndView(FormGroupFinalEntity rootFinalGroup, List<FormGroupFinalEntity> finalEntities,
                                                      List<FormViewPubEntity> viewPubEntities, Map<String, FormGroupFinalEntity> finalEntityMap) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        //finalEntities 过滤出根节点
        List<FormGroupFinalEntity> childrenFinalGroups =
                finalEntities.stream().filter(finalEntity -> rootFinalGroup.getId().equals(finalEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(childrenFinalGroups)) {
            //递归查询子节点
            for (FormGroupFinalEntity childrenFinalGroup : childrenFinalGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(childrenFinalGroup.getId());
                groupVo.setCode(childrenFinalGroup.getCode());
                groupVo.setName(childrenFinalGroup.getName());
                groupVo.setPath(childrenFinalGroup.getPath());
                groupVo.setRemark(childrenFinalGroup.getRemark());
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setParentId(childrenFinalGroup.getParentId());
                String parentName = StringUtil.isNullOrEmpty(childrenFinalGroup.getParentId()) ? "" : finalEntityMap.get(childrenFinalGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setChildren(getChildrenGroupAndView(childrenFinalGroup, finalEntities, viewPubEntities, finalEntityMap));
                groupVos.add(groupVo);
            }
        }
        //viewPubEntities 过滤出根节点
        List<FormViewPubEntity> childrenFinalViews =
                viewPubEntities.stream().filter(viewPubEntity -> rootFinalGroup.getId().equals(viewPubEntity.getGroupId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(childrenFinalViews)) {
            //递归查询子节点
            for (FormViewPubEntity childrenFinalView : childrenFinalViews) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(childrenFinalView.getId());
                groupVo.setCode(childrenFinalView.getCode());
                groupVo.setName(childrenFinalView.getName());
                groupVo.setType(GroupTypeEnums.VIEW.getCode());
                groupVo.setTypeName(GroupTypeEnums.VIEW.getValue());
                groupVo.setParentId(childrenFinalView.getGroupId());
                String parentName = StringUtil.isNullOrEmpty(childrenFinalView.getGroupId()) ? "" : finalEntityMap.get(childrenFinalView.getGroupId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setPubView(childrenFinalView);
                groupVo.setChildren(getChildrenView(childrenFinalView, viewPubEntities));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询视图子节点
     * @param view            定版视图实体
     * @param viewPubEntities 定版视图实体集合
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenView(FormViewPubEntity view, List<FormViewPubEntity> viewPubEntities) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormViewPubEntity> children = viewPubEntities.stream().filter(viewEntity -> view.getId().equals(viewEntity.getViewId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(children)) {
            for (FormViewPubEntity child : children) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(child.getId());
                groupVo.setCode(child.getCode());
                groupVo.setName(child.getName());
                groupVo.setType(GroupTypeEnums.SUB_VIEW.getCode());
                groupVo.setTypeName(GroupTypeEnums.SUB_VIEW.getValue());
                groupVo.setParentId(view.getId());
                groupVo.setPubView(child);
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }
}